<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\api\logic;



use app\common\model\AppAccountLog;
use app\common\model\BetsLog;
use app\common\model\BonusLog;
use app\common\model\EthAccount;
use app\common\model\EthAccountLog;
use app\common\model\GamblerKing;
use app\common\model\GamblerKingInvestmentUser;
use app\common\model\GamesDailyLog;
use app\common\model\GamesLog;
use app\common\model\KingAccountLog;
use app\common\model\Lottery;
use app\common\model\LotteryPlay;
use app\common\model\LotteryPlayType;
use app\common\model\LotteryResult;
use app\common\model\LotteryRound;
use app\common\model\LotteryType;
use app\common\model\Member;
use app\common\model\PlanProgramme;
use app\common\model\PlanUserNumber;
use app\common\model\PlanUserProgramme;
use app\common\model\PlanUserThrow;
use app\common\model\UserAccount;
use app\common\model\UserAccountLog;
use app\common\model\UserKingInvestment;
use app\common\model\Wallet;
use app\common\model\WithdrawalPayinfo;
use think\Db;

/**
 * 用户接口逻辑
 */
class Plan extends ApiBase
{
    

    /***
     * 自选号码
     */
    public function optional_number($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $play=LotteryPlay::get($data['play_id']);
        if(!$play){
            return ajaxreturn(1,lang('玩法错误'));
        }
        if($play['is_old']==1){
            empty($data['title']) && $data['title']==$play->playcate->data['name'].'-'.$play->data['name'];
        }else{
            empty($data['title']) && $data['title']=$play->data['name'];
        }

        $lottery=LotteryType::get($data['lottery_id']);
        if(!$lottery){
            return ajaxreturn(1,lang('彩种分类错误'));
        }
        if($play->is_old==1){
            $counts=1;
        }else{
            $cate_symbol=$play->cate->symbol;
            include_once(ROOT_PATH.'extend/yzwanfa/'.$cate_symbol.'.php');
            $_zhushuyzclass = "Lib\\yzwanfa\\{$cate_symbol}";
            $play_obj= new $_zhushuyzclass();
            $counts=$play_obj->checkzhushu($play->symbol,$data['number']);
            if(empty($counts)){
                return ajaxreturn(1,lang('下注参数错误'));
            }

        }
        $data['injection_number']=$counts;
        $data['user_id']=$uid;
        $res=$this->modelPlanUserNumber->setInfo($data);
        if(!empty($data['id'])){
            $res=$data['id'];
        }
        $info=$this->modelPlanUserNumber->getInfo(['id'=>$res]);
        return $info;


    }

    /***
     * 自选号码列表
     */
    public function optional_number_list($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        $list= $this->modelPlanUserNumber->getList(['user_id'=>$uid,'status'=>1],'*','id desc');
        foreach ($list as $k=>$v){
            $list[$k]['lottery_name']=$v->lottery->data['name'];
            $list[$k]['play_name']=$v->play->data['name'];
            unset($v->lottery);
            unset($v->play);
        }
        return $list;
    }

    /***
     * 简单倍投
     */
    public function simple_double_throw($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        if(empty($data['title'])){
            return ajaxreturn(1,lang('倍投名称不能为空'));
        }
        if(empty($data['number'])){
            return ajaxreturn(1,lang('倍投内容不能为空'));
        }
        $number=explode(' ',$data['number']);
        $max_bureaus=0;
        foreach ($number as $k=>$v){
            $v>$max_bureaus && $max_bureaus=$v;
            $arr=[
                'count'=>$k+1,
                'multiple'=>$v,
                'bureaus'=>$k+1,
                'bureaus_two'=>$k+1,
            ];
            $datas[]=$arr;
        }
        $data['plan_count']=count($number);
        $data['content']=json_encode($datas);
        $data['is_user']=2;
        $data['user_id']=$uid;

        $data['max_multiple']=$max_bureaus;
        $this->modelPlanUserThrow->setInfo($data);
        return ajaxreturn(0,lang('保存成功'));
    }

    /**
     * 进阶倍投
     */

    public function advanced_double_throw($data){
        $uid=decoded_user_token($data['user_token'],'member_id');
        if(empty($data['title'])){
            return ajaxreturn(1,lang('倍投名称不能为空'));
        }
        if(empty($data['content'])){
            return ajaxreturn(1,lang('倍投内容不能为空'));
        }
        $number=html_change($data['content']);
        $number=json_decode($number,true);
        $max_bureaus=0;
        $data['plan_count']=count($number);
        foreach ($number as $k=>$v){
            $v['multiple']>$max_bureaus && $max_bureaus=$v['multiple'];
            if($v['bureaus'] > $data['plan_count'] ||  $v['bureaus_two']> $data['plan_count'] ){
                return ajaxreturn(1,lang('倍投参数错误'));
            }
            $arr=[
                'count'=> $v['count'],
                'multiple'=>$v['multiple'],
                'bureaus'=>$v['bureaus'],
                'bureaus_two'=>$v['bureaus_two'],
            ];
            $datas[]=$arr;
        }
        $data['plan_count']=count($number);
        $data['content']=json_encode($datas);
        $data['is_user']=2;
        $data['user_id']=$this->logicApiBase->member['id'];
        $data['user_id']=$uid;
        $data['max_multiple']=$max_bureaus;
        $this->modelPlanUserThrow->setInfo($data);
        return ajaxreturn(0,lang('保存成功'));
    }

    /***
     * 我的倍投/大师倍投列表
     */
    public function my_double_throw($data){

        if($data['type']==1){
            $uid=decoded_user_token($data['user_token'],'member_id');
            $where=['user_id'=>$uid,'status'=>1,'is_user'=>2];
        }else{
            $where=['is_user'=>1,'status'=>1];
        }
        $list= $this->modelPlanUserThrow->getList($where,'*','id desc');
        foreach ($list as $k=>$v){
            $v->content;
            $list[$k]['image']=$v->image;

        }
        return $list;
    }

    /***
     *倍投详情
     */
    public function double_throw_detail($data){


        $info= PlanUserThrow::get($data['id']);
        $info->content;
        $info['image']=$info->image;

        return $info;
    }


    /***
     * 删除号码
     */
    public function del_double_number($data){
        $member=$this->logicApiBase->member;
        $info=PlanUserNumber::get($data['id']);
        if(!$info){
            return ajaxreturn(1,lang('非法操作'));
        }
        if($info['user_id']!=$member['id']){
            return ajaxreturn(1,lang('非法操作'));
        }
        $info->status=-1;
        $info->save();
        return ajaxreturn(0,lang('操作成功'));
    }


    /***
     * 删除倍投
     */
    public function del_double_throw($data){
        $member=$this->logicApiBase->member;
        $info=PlanUserThrow::get($data['id']);
        if(!$info){
            return ajaxreturn(1,lang('非法操作'));
        }
        if($info['user_id']!=$member['id']){
            return ajaxreturn(1,lang('非法操作'));
        }
        $info->status=-1;
        $info->save();
        return ajaxreturn(0,lang('操作成功'));
    }


    /**
     * 自选挂机
     */
    public function optional_hang($data){
        $member=$this->logicApiBase->member;
        $lottery=Lottery::get($data['lottery_id']);
        if(!$lottery){
            return ajaxreturn(1,lang('彩种错误'));
        }
        if(empty($data['round'])){
            return ajaxreturn(1,lang('期数不能为空'));
        }
        if($data['round']<0){
            return ajaxreturn(1,lang('期数不能为负数'));
        }
        if(empty($data['mult'])){
            return ajaxreturn(1,lang('倍数不能为空'));
        }
        if($data['mult']<0){
            return ajaxreturn(1,lang('倍数不能为负数'));
        }
        if(empty($data['univalent'])){
            return ajaxreturn(1,lang('单注金额不能为空'));
        }
        if($data['univalent']<0){
            return ajaxreturn(1,lang('单注金额不能为负数'));
        }
        $throw=PlanUserThrow::get($data['throw_id']);
        if(!$throw){
            return ajaxreturn(1,lang('倍投参数错误'));
        }
        $data['is_user']=$throw['is_user'];
        $number=PlanUserNumber::get($data['number_id']);
        if(!$number){
            return ajaxreturn(1,lang('号码参数错误'));
        }
        $data['number']=$number['number'];
        $data['play_id']=$number['play_id'];
        $data['injection_number']=$number['injection_number'];

        if($data['is_stop_time']){
            if(!empty($data['start_time']) && !empty($data['end_time']) ){
                $data['start_time']=strtotime($data['start_time']);
                $data['end_time']=strtotime($data['end_time']);
            }
        }

        if($data['is_stop_win']){

            if($data['stop_win_money']<0){
                return ajaxreturn(1,lang('止盈金额不能为负数'));
            }
        }
        if($data['is_stop_lose']){
            if($data['stop_lose_money']<0){
                return ajaxreturn(1,lang('止损金额不能为负数'));
            }
        }
        if(!empty($data['user_investment_id']) && $data['is_simulation'] !=1){
            $investment=GamblerKingInvestmentUser::get($data['user_investment_id']);
            $king_id=$investment['king_id'];
            $order_count=$investment->order_count;
            $data['king_id']=$king_id;
            $data['entrust_count']=$order_count;
        }
        $data['round_money']=$data['univalent']*$data['mult']*$data['injection_number'];
        $data['total']=$data['round_money']*$data['round'];

        $data['state']=2;
        $data['user_id']=$member['id'];
        if(empty($data['programme_id']) && empty($data['id'])){
            $res=$this->modelPlanProgramme->setInfo($data);
            $data['programme_id']=$res;
        }else{
            if(empty($data['id'])){
                //使用次数+1
                PlanProgramme::where(['id'=> $data['programme_id']])->setInc('use_count',1);
            }

        }
        if($data['is_simulation']==1){
            $data['state']=1;
        }


        $this->modelPlanUserProgramme->setInfo($data);
        return ajaxreturn(0,lang('操作成功'));

    }

    /***
     * 修改挂机
     */
    public function optional_hang_edit($data){
        if($data['is_stop_time']){
            if(!empty($data['start_time']) && !empty($data['end_time']) ){
                $data['start_time']=strtotime($data['start_time']);
                $data['end_time']=strtotime($data['end_time']);
            }
        }

        if($data['is_stop_win']){

            if($data['stop_win_money']<0){
                return ajaxreturn(1,lang('止盈金额不能为负数'));
            }
        }
        if($data['is_stop_lose']){
            if($data['stop_lose_money']<0){
                return ajaxreturn(1,lang('止损金额不能为负数'));
            }
        }
        $this->modelPlanUserProgramme->setInfo($data);
        return ajaxreturn(0,lang('操作成功'));
    }


    /***
     *方案详情
     */
    public function  optional_hang_detail($data){
       $info=PlanProgramme::get($data['id']);
       if($info['is_stop_time']){
            $info['start_time']=date('Y-m-d H:i:s',$info['start_time']);
           $info['end_time']=date('Y-m-d H:i:s',$info['end_time']);
       }
       $info['image']=$info->lottery->image;
        $info['lottery_name']=$info->lottery->data['name'];
        $info['lottery_image']=$info->lottery->image;
        $info['number_name']=$info->bets->title;
        $info['doubule_name']=$info->doubule->title;
        $info['play_name']=$info->play->data['name'];
        unset($info->lottery);
        unset($info->play);
        unset($info->bets);
       return $info;



    }


    /***
     * 我的方案
     */
    public function my_programme($data){


        $uid=decoded_user_token($data['user_token'],'member_id');
        $where=['user_id'=>$uid,'status'=>1];
        $list= $this->modelPlanProgramme->getList($where,'*','id desc');
        foreach ($list as $k=>$v){
            $list[$k]['image']=$v->lottery->image;
            $list[$k]['lottery_name']=$v->lottery->data['name'];
            $list[$k]['number_name']=$v->bets->title;
            $list[$k]['play_name']=$v->play->data['name'];
            unset($v->lottery);
            unset($v->bets);
            unset($v->play);
        }
        return $list;
    }

    /***
     * 设置我的方案
     */

    public function set_my_programme($data){
        if(!empty($data['is_share'])){
            if($data['is_share']!=1 && $data['is_share']!=2){
                return ajaxreturn(1,lang('参数错误'));
            }
        }else{
            if(empty($data['id'])){
                return ajaxreturn(1,lang('参数错误'));
            }
            $number=PlanUserNumber::get($data['number_id']);
            if(!$number){
                return ajaxreturn(1,lang('号码参数错误'));
            }
            $throw=PlanUserThrow::get($data['throw_id']);
            if(!$throw){
                return ajaxreturn(1,lang('倍投参数错误'));
            }
            $data['number']=$number['number'];
            $data['play_id']=$number['play_id'];
            $data['injection_number']=$number['injection_number'];
            if($data['is_stop_time']){
                if(!empty($data['start_time']) && !empty($data['end_time']) ){
                    $data['start_time']=strtotime($data['start_time']);
                    $data['end_time']=strtotime($data['end_time']);
                }
            }
            if($data['is_stop_win']){

                if($data['stop_win_money']<0){
                    return ajaxreturn(1,lang('止盈金额不能为负数'));
                }
            }
            if($data['is_stop_lose']){
                if($data['stop_lose_money']<0){
                    return ajaxreturn(1,lang('止损金额不能为负数'));
                }
            }
            if(!empty($data['user_investment_id']) && $data['is_simulation'] !=1){
                $investment=GamblerKingInvestmentUser::get($data['user_investment_id']);
                $king_id=$investment['king_id'];
                $order_count=$investment->order_count;
                $data['king_id']=$king_id;
                $data['entrust_count']=$order_count;
            }
            $data['round_money']=$data['univalent']*$data['mult']*$data['injection_number'];
            $data['total']=$data['round_money']*$data['round'];
        }
        $this->modelPlanProgramme->setInfo($data);
        return ajaxreturn(0,lang('操作成功'));

    }

    /***
     * 启动关闭挂机
     */

    public function start_hang_up($data){

        if($data['state']!=1 && $data['state']!=3){
            return ajaxreturn(1,lang('参数错误'));
        }
        $data['has_start_time']=TIME_NOW;
        $this->modelPlanUserProgramme->setInfo($data);
        return ajaxreturn(0,lang('操作成功'));

    }

    /***
     * 云方案
     */
    public function cloud_solutions($data){


        $where=['is_share'=>1,'status'=>1];
        !empty($data['lottery_id'])  && $where['lottery_id']=$data['lottery_id'];
        !empty($data['play_id'])  && $where['play_id']=$data['play_id'];
        !empty($data['is_lately'])  && $where['lottery_id']=3;
        $list= $this->modelPlanProgramme->getList($where,'*','id desc');
        foreach ($list as $k=>$v){
            $list[$k]['image']=$v->lottery->image;
            $list[$k]['lottery_name']=$v->lottery->data['name'];
            $list[$k]['number_name']=$v->bets->title;
            $list[$k]['play_name']=$v->play->data['name'];
            unset($v->lottery);
            unset($v->bets);
            unset($v->play);
        }
        return $list;
    }


    /***
     * 挂机明细
     */
    public function hang_up_list($data){


        $info=PlanUserProgramme::get($data['id']);
        $list=[];
        if($info){

            $where['programme_id']=$data['id'];
            if($info['is_simulation']==1){
                $where['state']=['neq',4];
                $list= $this->modelBetsLogSubscribe->getList($where,'*','id asc');
                foreach ($list as $k=>$v){
                    $list[$k]['play_name']=$v->play->data['name'];
                    unset($v->play);
                }
            }else{
                $where['state']=['neq',4];
                $list= $this->modelBetsLog->getList($where,'*','id asc');
                foreach ($list as $k=>$v){
                    $list[$k]['state']=$v->game->state;
                    $list[$k]['is_winner']=$v->game->is_winner;
                    $list[$k]['profit']=$v->game->profit;
                    $list[$k]['play_name']=$v->play->data['name'];
                    unset($v->game);
                    unset($v->play);
                }
            }

        }
        return $list;
    }

    /***
     * 我的挂机
     */
    public function my_hang_up($data){

        $uid=decoded_user_token($data['user_token'],'member_id');
        $where=['user_id'=>$uid];
        if(!empty($data['type'])){
            $where['state']=['in',[1,2,3]];
        }else{
            $where['state']=4;
        }

        $list= $this->modelPlanUserProgramme->getList($where,'*','id asc');
        foreach ($list as $k =>$v){
            if($v['has_end_time']){
                $list[$k]['down_time']=$v['has_end_time']-$v['has_start_time'];
            }else{
                $list[$k]['down_time']=TIME_NOW-$v['has_start_time'];
            }
            $list[$k]['number_name']=$v->bets->title;
            if($v->bets_money){
                $list[$k]['profit_rate']=format_money($v->current_win_money/$v->bets_money,2);
            }else{
                $list[$k]['profit_rate']=0;
            }
            $list[$k]['start_time']=date('Y-m-d H:i:s',$v['start_time']);
            $list[$k]['end_time']=date('Y-m-d H:i:s',$v['end_time']);
            $list[$k]['surplus_round']=$v->round-$v->current_round;
            unset($v->bets);

        }

        return $list;
    }



    /***
     * 挂机今日数据接口
     */
    public function  today_lottery_bet($data){
        $uid=decoded_user_token($data['user_token'],'member_id');

        $lottery=Lottery::get($data['lottery_id']);
        if(!$lottery){
            return ajaxreturn(1,lang('彩种错误'));
        }
        LotteryRound::where("start_time <= unix_timestamp(now()) and state=1 ")->update(["state" => 2]);
        LotteryRound::where("end_time < unix_timestamp(now()) ")->update(["state" => 3]);
        $res_info=LotteryRound::where(['status'=>1,'lottery_id'=>$data['lottery_id'],'state'=>2])->order('id desc')->find();
        if(!$res_info){
            $res_info=LotteryRound::where(['status'=>1,'lottery_id'=>$data['lottery_id'],'state'=>1,'start_time'=>['gt',TIME_NOW]])->order('id asc')->find();
        }
        if($res_info){
            $res_info['start_time_down']=$res_info->getData('start_time')-TIME_NOW;
            $res_info['end_time_down']=$res_info->getData('end_time')-TIME_NOW;
        }
        $where['a.status']=1;
        !empty($data['lottery_id'])  && $where['a.lottery_id']=$data['lottery_id'];
        $this->modelLotteryResult->alias('a');
        $lottery_res=$this->modelLotteryResult->where($where)->order('id desc')->find();
//        $lottery_res['number']=explode(',',$lottery_res['number']);
        $lottery_res['add_time']=explode(',',$lottery_res->getData('create_time'));

        $info['today_test_reward']=UserAccountLog::where(['type'=>32,'user_id'=>$uid,'lottery_id'=>$data['lottery_id'],'create_time'=>['between',[getTodayStart(),TIME_NOW]]])->sum('number')+0;
        $info['today_bets']=BetsLog::where(['user_id'=>$uid,'lottery_id'=>$data['lottery_id'],'create_time'=>['between',[getTodayStart(),TIME_NOW]],'entrust_count'=>0,'programme_id'=>['gt',0]])->sum('money')+0;
        $info['today_profit']=BetsLog::where(['user_id'=>$uid,'lottery_id'=>$data['lottery_id'],'create_time'=>['between',[getTodayStart(),TIME_NOW]],'entrust_count'=>0,'programme_id'=>['gt',0]])->sum('reward_money')- BetsLog::where(['user_id'=>$uid,'state'=>['gt',1],'lottery_id'=>$data['lottery_id'],'create_time'=>['between',[getTodayStart(),TIME_NOW]],'entrust_count'=>0,'programme_id'=>['gt',0]])->sum('money')+0;
        $info['today_profit']=format_money( $info['today_profit'],2);
        $info['lottery_info']=$res_info;
        $info['lottery_result']=$lottery_res;
        return $info;


    }







}